package com.mytrans.aspect;

import com.mytrans.client.MytransServerClient;
import com.mytrans.transaction.TokenManager;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;

@Aspect
public class TranceAspect {

    @Autowired
    private TokenManager tokenManager;
    @Autowired
    private MytransServerClient myTransServerClient;

    @Pointcut("within(@com.mytrans.aspect.Mytrans *)")
    public void cutController() {
    }

    /**
     * 切点
     */
    @Around("cutController()")
    public Object cutPoint(ProceedingJoinPoint point) throws Throwable {
        String token = tokenManager.initToken();
        try{
            Object returnObj = point.proceed();
            myTransServerClient.callCommit(token);
            return returnObj;
        } catch (Exception e) {
            myTransServerClient.callRollBack(token);
            throw e;
        }
    }
}
